home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dialupip / dialupip2.0 / src / dulib / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-11  |  4.0 KB  |  165 lines

  1. /*
  2. **  Logging routines.
  3. **  Copyright (c) 1991 Bolt Beranek and Newman, Inc.
  4. **  All rights reserved.
  5. **
  6. **  Redistribution and use in source and binary forms are permitted
  7. **  provided that: (1) source distributions retain this entire copyright
  8. **  notice and comment, and (2) distributions including binaries display
  9. **  the following acknowledgement:  ``This product includes software
  10. **  developed by Bolt Beranek and Newman, Inc. and CREN/CSNET'' in the
  11. **  documentation or other materials provided with the distribution and in
  12. **  all advertising materials mentioning features or use of this software.
  13. **  Neither the name of Bolt Beranek and Newman nor CREN/CSNET may be used
  14. **  to endorse or promote products derived from this software without
  15. **  specific prior written permission.
  16. **
  17. **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. **  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. #include <stdio.h>
  22. #include <varargs.h>
  23. #include <time.h>
  24. #include <sys/types.h>
  25. #include "dialupip.h"
  26.  
  27.  
  28. /*
  29. **  Current logging level.
  30. */
  31. int    log_level = DLOG_GENERAL;
  32.  
  33.  
  34. /*
  35. **  Internal logging routine.
  36. */
  37. static void
  38. d_vlog(where, format, ap)
  39.     char        *where;
  40.     char        *format;
  41.     va_list        ap;
  42. {
  43.     extern time_t    time();
  44.     extern struct tm    *localtime();
  45.     extern char        *strerror();
  46.     extern int        errno;
  47.     static char        CONSOLE[] = "/dev/console";
  48.     static char        LOG[] = LOG_FILE;
  49.     FILE        *F;
  50.     struct tm        *tp;
  51.     time_t        t;
  52.     char        form[30];
  53.     char        *fp;
  54.     char        c;
  55.     int            oerrno;
  56.     int            mvalue;
  57.     int            islong;
  58.     int            done;
  59.  
  60.     /* Save errno for %m. */
  61.     mvalue = errno;
  62.  
  63.     /* Open the log file */
  64.     if ((F = fopen(LOG, "a")) == NULL) {
  65.     oerrno = errno;
  66.     if ((F = fopen("/dev/console", "a")) == NULL) {
  67.         (void)fprintf(stderr, "%s:  Can't open logfile \"%s\",\n\t%s\n",
  68.             progname, LOG, strerror(oerrno));
  69.         return;
  70.     }
  71.     (void)fprintf(stderr,
  72.         "%s:  Can't open logfile \"%s\",\n\t%s, using \"%s\"\n",
  73.         progname, LOG, strerror(oerrno), CONSOLE);
  74.     }
  75.  
  76.     /* Get the timestamp, write the message header. */
  77.     (void)time(&t);
  78.     tp = localtime(&t);
  79.     (void)fprintf(F, "%02d/%02d %02d:%02d:%02d\t%d\t%s: ",
  80.     tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec,
  81.     getpid(), where);
  82.  
  83.     while (c = *format++) {
  84.     if (c != '%') {
  85.         (void)fputc(c, F);
  86.         continue;
  87.     }
  88.     if (*format == '%') {
  89.         (void)fputc('%', F);
  90.         format++;
  91.         continue;
  92.     }
  93.  
  94.     /* A standard printf format. copy it into form. */
  95.     for (islong = 0, fp = form, *fp++ = c, done = 0; done == 0; )
  96.         switch (c = *format++) {
  97.         default:
  98.         *fp++ = c;
  99.         break;
  100.         case 'o': case 'c': case 'd': case 's': case 'u':
  101.         case 'e': case 'E': case 'f': case 'g': case 'G':
  102.         case 'm':
  103.         done++;
  104.         /* FALLTHROUGH */
  105.         case ' ': case '+': case '-': case '.': case '#':
  106.         case '0': case '1': case '2': case '3': case '4':
  107.         case '5': case '6': case '7': case '8': case '9':
  108.         *fp++ = c;
  109.         break;
  110.         case 'l':
  111.         islong++;
  112.         *fp++ = c;
  113.         break;
  114.         }
  115.     *fp = '\0';
  116.  
  117.     /* We have the format string, get the argument and print it. */
  118.     switch (fp[-1]) {
  119.     case 'x': case 'o': case 'c': case 'd': case 'u':
  120.         if (islong)
  121.         (void)fprintf(F, form, va_arg(ap, long));
  122.         else
  123.         (void)fprintf(F, form, va_arg(ap, int));
  124.         break;
  125.     case 'e': case 'E': case 'f': case 'g': case 'G':
  126.         (void)fprintf(F, form, va_arg(ap, double));
  127.         break;
  128.     case 's':
  129.         (void)fprintf(F, form, va_arg(ap, char*));
  130.         break;
  131.     case 'm':
  132.         fp[-1] = 's';
  133.         (void)fprintf(F, form, strerror(mvalue));
  134.         break;
  135.     }
  136.     }
  137.     (void)fputc('\n', F);
  138.     (void)fclose(F);
  139. }
  140.  
  141.  
  142.  
  143. /*
  144. **  Write debug log entries.  Calling sequence:
  145. **    d_log(level, where, format, args...)
  146. */
  147. /* VARARGS0 */
  148. void
  149. d_log(va_alist)
  150.     va_dcl
  151. {
  152.     int        level;
  153.     char    *where;
  154.     char    *format;
  155.     va_list    ap;
  156.  
  157.     va_start(ap);
  158.     level = va_arg(ap, int);
  159.     where = va_arg(ap, char*);
  160.     format = va_arg(ap, char*);
  161.     if (level <= log_level)
  162.     d_vlog(where, format, ap);
  163.     va_end(ap);
  164. }
  165.